//
//  XMSSQLiteHelper+XMSSQLiteTableCol.m
//  etionMisc
//
//  Created by wjxfhxy on 15/11/2.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "XMSSQLiteHelper+XMSSQLiteTableCol.h"

@interface XMSTableCol()

@property(nonatomic,retain) NSString *colName;
@property(nonatomic,retain) NSString *colTypeText;
@property(nonatomic,assign) BOOL primaryKey;
@property(nonatomic,assign) BOOL notNull;
@property(nonatomic,retain) NSString *defaultValue;

@end

@implementation XMSTableCol

-(void)dealloc
{
    [_colName release_mm];
    [_colTypeText release_mm];
    [_defaultValue release_mm];
    superdealloc_mm;
}

@end


@implementation XMSSQLiteHelper (XMSSQLiteTableCol)

- (NSArray *)SQLiteTableHeads:(NSString *)tableName
{
    __block NSString *createSQL = @"";
    NSString *sql = [NSString stringWithFormat:@"select sql from sqlite_master where type='table' and name='%@'", tableName];
    [self selectTable:[sql UTF8String] gettingTable:^(BOOL *bStop)
    {
        createSQL = [self getFieldNSString:0];
    }];
    
 
    NSMutableArray *tableCols = nil;
    
    NSMutableCharacterSet *set = [[NSMutableCharacterSet new] autorelease_mm];
    [set addCharactersInString:@"("];
    [set addCharactersInString:@")"];
    NSArray *ar = [createSQL componentsSeparatedByCharactersInSet:set];

    ar = [((NSString *) [ar objectAtIndex:1]) componentsSeparatedByString:@","];
    if (ar.count > 0)
    {
        tableCols = [[NSMutableArray new] autorelease_mm];
        
        for (NSString *s in ar)
        {
            s = [s lowercaseString];
            
            XMSTableCol *tableCol = [[XMSTableCol new] autorelease_mm];
            [tableCols addObject:tableCol];
            
            if([s rangeOfString:@"primary key"].location != NSNotFound)
                tableCol.primaryKey = YES;
            
            NSString *colName = @"";
            NSString *colTypeText = @"";
            if([self fetchString:s colNameAndColTypeTextToTag:@"text" outColName:&colName outColTypeText:&colTypeText])
                ;
            else if([self fetchString:s colNameAndColTypeTextToTag:@"integer" outColName:&colName outColTypeText:&colTypeText])
                ;
            else if([self fetchString:s colNameAndColTypeTextToTag:@"real" outColName:&colName outColTypeText:&colTypeText])
                ;
            
            tableCol.colName = colName;
            tableCol.colTypeText = [colTypeText uppercaseString];
        }
    }
    
    return tableCols;
}

-(BOOL)fetchString:(NSString*)str colNameAndColTypeTextToTag:(NSString*)tag outColName:(NSString**)outColName outColTypeText:(NSString**)outColTypeText
{
    NSRange range = [str rangeOfString:tag];
    if(range.location != NSNotFound)
    {
        NSString *colName = [str substringToIndex:range.location];
        NSString *colTypeText = [str substringWithRange:NSMakeRange(range.location, range.length)];
        
        colName = [colName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        NSMutableCharacterSet *set = [[NSMutableCharacterSet new] autorelease_mm];
        [set addCharactersInString:@"\""];
        [set addCharactersInString:@"["];
        [set addCharactersInString:@"]"];
        colName = [colName stringByTrimmingCharactersInSet:set];
        
        colTypeText = [colTypeText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        
        *outColName = colName;
        *outColTypeText = colTypeText;
        
        return YES;
    }
    return NO;
}

@end
